
package com.forezp.service;

import com.forezp.client.UserServiceClient;
import com.forezp.dao.BlogDao;
import com.forezp.dto.BlogDetailDTO;
import com.forezp.dto.RespDTO;
import com.forezp.entity.Blog;
import com.forezp.entity.User;
import com.forezp.exception.CommonException;
import com.forezp.exception.ErrorCode;
import com.forezp.util.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BlogService {

	@Autowired
	private BlogDao blogDao;

	@Autowired
	private UserServiceClient userServiceClient;

	public Blog postBlog(Blog blog) {
		return blogDao.save(blog);
	}

	public List<Blog> findBlogs(String username) {
		return blogDao.findByUsername(username);
	}

	public BlogDetailDTO findBlogDetail(Long id) {
		Blog blog = blogDao.findOne(id);
		if (null == blog) {
			throw new CommonException(ErrorCode.BLOG_IS_NOT_EXIST);
		}
		RespDTO<User> respDTO = userServiceClient.getUser(UserUtils.getCurrentToken(), blog.getUsername());
		if (respDTO == null) {
			throw new CommonException(ErrorCode.RPC_ERROR);
		}
		BlogDetailDTO blogDetailDTO = new BlogDetailDTO();
		blogDetailDTO.setBlog(blog);
		blogDetailDTO.setUser(respDTO.data);
		return blogDetailDTO;
	}
}